package com.playball.lottery.fragment.frm;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils.TruncateAt;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.playball.lottery.ApplicationData;
import com.playball.lottery.JddMainActivity;
import com.playball.lottery.R;
import com.playball.lottery.customview.BallDTO;
import com.playball.lottery.eventmanger.EventManager;
import com.playball.lottery.eventmanger.EventType;
import com.playball.lottery.frm.ads.AdvZx;
import com.playball.lottery.frm.buyhall.BDActivity;
import com.playball.lottery.frm.buyhall.CQSSCActivity;
import com.playball.lottery.frm.buyhall.CQSSCListActivity;
import com.playball.lottery.frm.buyhall.CtzqRx9Activity;
import com.playball.lottery.frm.buyhall.CtzqSfcActivity;
import com.playball.lottery.frm.buyhall.DltxhActivity;
import com.playball.lottery.frm.buyhall.DltxhlbActivity;
import com.playball.lottery.frm.buyhall.ElvSelFiveActivity;
import com.playball.lottery.frm.buyhall.ElvSelFiveListActivity;
import com.playball.lottery.frm.buyhall.ElvYunDJActivity;
import com.playball.lottery.frm.buyhall.ElvYunDJListActivity;
import com.playball.lottery.frm.buyhall.Fc3dListActivity;
import com.playball.lottery.frm.buyhall.Fc3dxhActivity;
import com.playball.lottery.frm.buyhall.JcBasketballActivity;
import com.playball.lottery.frm.buyhall.JcfootballActivity;
import com.playball.lottery.frm.buyhall.Kuai3Activity;
import com.playball.lottery.frm.buyhall.Kuai3ListActivity;
import com.playball.lottery.frm.buyhall.N7lxhActivity;
import com.playball.lottery.frm.buyhall.N7lxhlbActivity;
import com.playball.lottery.frm.buyhall.N7xxhActivity;
import com.playball.lottery.frm.buyhall.N7xxhlbActivity;
import com.playball.lottery.frm.buyhall.Pailie3ListActivity;
import com.playball.lottery.frm.buyhall.Pailie3xhActivity;
import com.playball.lottery.frm.buyhall.Pailie5ListActivity;
import com.playball.lottery.frm.buyhall.Pailie5xhActivity;
import com.playball.lottery.frm.buyhall.SixFuckActivity;
import com.playball.lottery.frm.buyhall.SsqxhActivity;
import com.playball.lottery.frm.buyhall.SsqxhlbActivity;
import com.playball.lottery.nt.GetRequestParams;
import com.playball.lottery.util.Constants;
import com.playball.lottery.util.ConvertJsonToList;
import com.playball.lottery.util.LotConstants;
import com.playball.lottery.util.LotUtil;
import com.playball.lottery.util.PreferencesUtils;
import com.playball.lottery.util.cache.MyPreference;
import com.playball.lottery.widget.PullToRefresh.PullToRefreshListView;


public class HomeFragment extends Fragment implements LotConstants {
	public static final String TAG = "HomeFragment";


	private View _RootView;
	private TextView tv_title_left;
	private TextView tv_title_right;
	private PullToRefreshListView lv_ptrl_lot;
	private ListView lv_mListView;
	private PreferencesUtils pUtil;
	private TextView tv_title;
	private LotAdapter adapter;
	private List<Map<String, Object>> lotData;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		pUtil = new PreferencesUtils(getActivity(), Constants.PREFERENCES_NAME);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		_RootView = inflater.inflate(R.layout.ui_new_mainhome, null);
		// lunbolayout = inflater.inflate(R.layout.fragment_home_lunbo, null);
//		lunbo = new LunBoFragment(getActivity());
		return _RootView;
	}

	@Override
	public void onDestroyView() {
		// TODO Auto-generated method stub
//		lunbo.pauseLunbo();
		super.onDestroyView();

	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);

		tv_title = (TextView) _RootView.findViewById(R.id.top_title_info);
		tv_title.setEllipsize(TruncateAt.MIDDLE);

		tv_title_left = (TextView) _RootView.findViewById(R.id.top_title_back);
		tv_title_left.setVisibility(View.INVISIBLE);

		tv_title_right = (TextView) _RootView.findViewById(R.id.top_title_right_btn);

		lv_ptrl_lot = (PullToRefreshListView) _RootView.findViewById(R.id.lv_mh_ptrl_lot);
		lv_mListView = lv_ptrl_lot.getRefreshableView();

		// lv_ptrl_lot.setOnRefreshListener(new OnRefreshListener() {
		// @Override
		// public void onRefresh() {
		// // TODO Auto-generated method stub
		// if (lv_ptrl_lot.getRefreshType() == 1) { // 下拉
		// getLots();
		// }
		// }
		// });

		autoUserLogin();

		tv_title_right.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				getActivity().startActivity(new Intent(getActivity(), AdvZx.class));
			}
		});

		lv_mListView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				// TODO Auto-generated method stub
				Map<String, Object> item = (Map<String, Object>) lv_mListView.getAdapter().getItem(arg2);
				Intent intent = new Intent();
				switch (Integer.parseInt(item.get("LotID").toString())) {
					case LOT_ID_LHC:
						intent.setClass(getActivity(), SixFuckActivity.class);
						break;
					case LOT_ID_SSQ:
						int ssq_size = pUtil.getInt("ssq_size", 0);
						if (ssq_size != 0) {
							ArrayList<BallDTO> list = new ArrayList<BallDTO>();
							for (int i = 0; i < ssq_size; i++) {
								BallDTO ball = new BallDTO();
								ball.setSsq_ball(pUtil.getString("ssq_ball" + i, ""));
								list.add(ball);
							}
							intent.putExtra("list_ssqdto", list);
							intent.putExtra("AllMoney", pUtil.getInt("ssq_allMoney", 0));
							intent.setClass(getActivity(), SsqxhlbActivity.class);
							Toast.makeText(getActivity(), R.string.this_is_the_last_time_you_save_the_number, Toast.LENGTH_SHORT).show();
						} else {
							intent.setClass(getActivity(), SsqxhActivity.class);
						}
						break;
					case LOT_ID_DLT:
						int dlt_size = pUtil.getInt("dlt_size", 0);
						if (dlt_size != 0) {
							ArrayList<BallDTO> list = new ArrayList<BallDTO>();
							for (int i = 0; i < dlt_size; i++) {
								BallDTO ball = new BallDTO();
								ball.setDlt_ball(pUtil.getString("dlt_ball" + i, ""));
								list.add(ball);
							}
							intent.putExtra("list_dltdto", list);
							intent.putExtra("AllMoney", pUtil.getInt("dlt_allMoney", 0));
							intent.setClass(getActivity(), DltxhlbActivity.class);
							Toast.makeText(getActivity(), R.string.this_is_the_last_time_you_save_the_number, Toast.LENGTH_SHORT).show();
						} else {
							intent.setClass(getActivity(), DltxhActivity.class);
						}
						ApplicationData.getInstance().setIsDLTZhuiJiaClick(false);
						break;
					case LOT_ID_11X5:
						int esf_size = pUtil.getInt("esf_size", 0);
						if (esf_size != 0) {
							ArrayList<BallDTO> list = new ArrayList<BallDTO>();
							for (int i = 0; i < esf_size; i++) {
								BallDTO ball = new BallDTO();
								ball.setElv_ball(pUtil.getString("esf_ball" + i, ""));
								list.add(ball);
							}
							intent.putExtra("list_elvdto", list);
							intent.putExtra("AllMoney", pUtil.getInt("esf_allMoney", 0));
							intent.putExtra("PlayTypeID", pUtil.getInt("esf_play", 0));
							intent.putExtra("prizeIntellChase", pUtil.getInt("esf_prizeIntellChase", 0));
							intent.setClass(getActivity(), ElvSelFiveListActivity.class);
							Toast.makeText(getActivity(), R.string.this_is_the_last_time_you_save_the_number, Toast.LENGTH_SHORT).show();
						} else {
							intent.setClass(getActivity(), ElvSelFiveActivity.class);
						}
						break;
					case LOT_ID_JCZQ:
						intent.setClass(getActivity(), JcfootballActivity.class);
						break;
					case LOT_ID_BD:
						intent.setClass(getActivity(), BDActivity.class);
						break;
					case LOT_ID_JCLQ:
						intent.setClass(getActivity(), JcBasketballActivity.class);
						break;
					case LOT_ID_ZQ_SFC:
						intent.setClass(getActivity(), CtzqSfcActivity.class);
						break;
					case LOT_ID_ZQ_RX9:
						intent.setClass(getActivity(), CtzqRx9Activity.class);
						break;
					case LOT_ID_FC3D:
						int fc3d_size = pUtil.getInt("fc3d_size", 0);
						if (fc3d_size != 0) {
							ArrayList<BallDTO> list = new ArrayList<BallDTO>();
							for (int i = 0; i < fc3d_size; i++) {
								BallDTO ball = new BallDTO();
								ball.setPl3_ball(pUtil.getString("fc3d_ball" + i, ""));
								list.add(ball);
							}
							intent.putExtra("list_elvdto", list);
							intent.putExtra("AllMoney", pUtil.getInt("fc3d_allMoney", 0));
							intent.putExtra("PlayTypeID", pUtil.getInt("fc3d_play", 0));
							intent.setClass(getActivity(), Fc3dListActivity.class);
							Toast.makeText(getActivity(), R.string.this_is_the_last_time_you_save_the_number, Toast.LENGTH_SHORT).show();
						} else {
							intent.setClass(getActivity(), Fc3dxhActivity.class);
						}
						break;
					case LOT_ID_QLC:
						int n7lx_size = pUtil.getInt("n7lx_size", 0);
						if (n7lx_size != 0) {
							ArrayList<BallDTO> list = new ArrayList<BallDTO>();
							for (int i = 0; i < n7lx_size; i++) {
								BallDTO ball = new BallDTO();
								ball.setPl3_ball(pUtil.getString("n7lx_ball" + i, ""));
								list.add(ball);
							}
							intent.putExtra("list_elvdto", list);
							intent.putExtra("AllMoney", pUtil.getInt("n7lx_allMoney", 0));
							intent.putExtra("PlayTypeID", pUtil.getInt("n7lx_play", 0));
							intent.setClass(getActivity(), N7lxhlbActivity.class);
							Toast.makeText(getActivity(), R.string.this_is_the_last_time_you_save_the_number, Toast.LENGTH_SHORT).show();
						} else {
							intent.setClass(getActivity(), N7lxhActivity.class);
						}
						break;
					case LOT_ID_PL3:
						int pl3_size = pUtil.getInt("pl3_size", 0);
						if (pl3_size != 0) {
							ArrayList<BallDTO> list = new ArrayList<BallDTO>();
							for (int i = 0; i < pl3_size; i++) {
								BallDTO ball = new BallDTO();
								ball.setPl3_ball(pUtil.getString("pl3_ball" + i, ""));
								list.add(ball);
							}
							intent.putExtra("list_elvdto", list);
							intent.putExtra("AllMoney", pUtil.getInt("pl3_allMoney", 0));
							intent.putExtra("PlayTypeID", pUtil.getInt("pl3_play", 0));
							intent.setClass(getActivity(), Pailie3ListActivity.class);
							Toast.makeText(getActivity(), R.string.this_is_the_last_time_you_save_the_number, Toast.LENGTH_SHORT).show();
						} else {
							intent.setClass(getActivity(), Pailie3xhActivity.class);
						}
						break;
					case LOT_ID_PL5:
						int pl5_size = pUtil.getInt("pl5_size", 0);
						if (pl5_size != 0) {
							ArrayList<BallDTO> list = new ArrayList<BallDTO>();
							for (int i = 0; i < pl5_size; i++) {
								BallDTO ball = new BallDTO();
								ball.setPl5_ball(pUtil.getString("pl5_ball" + i, ""));
								list.add(ball);
							}
							intent.putExtra("list_elvdto", list);
							intent.putExtra("AllMoney", pUtil.getInt("pl5_allMoney", 0));
							intent.setClass(getActivity(), Pailie5ListActivity.class);
							Toast.makeText(getActivity(), R.string.this_is_the_last_time_you_save_the_number, Toast.LENGTH_SHORT).show();
						} else {
							intent.setClass(getActivity(), Pailie5xhActivity.class);
						}
						break;
					case LOT_ID_QXC:
						int n7xx_size = pUtil.getInt("n7xx_size", 0);
						if (n7xx_size != 0) {
							ArrayList<BallDTO> list = new ArrayList<BallDTO>();
							for (int i = 0; i < n7xx_size; i++) {
								BallDTO ball = new BallDTO();
								ball.setPl3_ball(pUtil.getString("n7xx_ball" + i, ""));
								list.add(ball);
							}
							intent.putExtra("list_elvdto", list);
							intent.putExtra("AllMoney", pUtil.getInt("n7xx_allMoney", 0));
							intent.putExtra("PlayTypeID", pUtil.getInt("n7xx_play", 0));
							intent.setClass(getActivity(), N7xxhlbActivity.class);
							Toast.makeText(getActivity(), R.string.this_is_the_last_time_you_save_the_number, Toast.LENGTH_SHORT).show();
						} else {
							intent.setClass(getActivity(), N7xxhActivity.class);
						}
						break;
					case LOT_ID_CQSSC:
						int cqssc_size = pUtil.getInt("cqssc_size", 0);
						if (cqssc_size != 0) {
							ArrayList<BallDTO> list = new ArrayList<BallDTO>();
							for (int i = 0; i < cqssc_size; i++) {
								BallDTO ball = new BallDTO();
								ball.setElv_ball(pUtil.getString("cqssc_ball" + i, ""));
								list.add(ball);
							}
							intent.putExtra("list_elvdto", list);
							intent.putExtra("AllMoney", pUtil.getInt("cqssc_allMoney", 0));
							intent.putExtra("PlayTypeID", pUtil.getInt("cqssc_play", 0));
							intent.putExtra("strPlayType", pUtil.getString("cqssc_type", ""));
							intent.putExtra("prizeIntellChase", pUtil.getInt("cqssc_prizeIntellChase", 0));
							intent.setClass(getActivity(), CQSSCListActivity.class);
							Toast.makeText(getActivity(), R.string.this_is_the_last_time_you_save_the_number, Toast.LENGTH_SHORT).show();
						} else {
							intent.setClass(getActivity(), CQSSCActivity.class);
						}
						break;
					case LOT_ID_11YDJ:
						int eydu_size = pUtil.getInt("eydu_size", 0);
						if (eydu_size != 0) {
							ArrayList<BallDTO> list = new ArrayList<BallDTO>();
							for (int i = 0; i < eydu_size; i++) {
								BallDTO ball = new BallDTO();
								ball.setElv_ball(pUtil.getString("eydu_ball" + i, ""));
								list.add(ball);
							}
							intent.putExtra("list_elvdto", list);
							intent.putExtra("AllMoney", pUtil.getInt("eydu_allMoney", 0));
							intent.putExtra("PlayTypeID", pUtil.getInt("eydu_play", 0));
							intent.putExtra("prizeIntellChase", pUtil.getInt("eydu_prizeIntellChase", 0));
							intent.setClass(getActivity(), ElvYunDJListActivity.class);
							Toast.makeText(getActivity(), R.string.this_is_the_last_time_you_save_the_number, Toast.LENGTH_SHORT).show();
						} else {
							intent.setClass(getActivity(), ElvYunDJActivity.class);
						}
						break;
					case LOT_ID_KUAI3:
						int kuai3_size = pUtil.getInt("kuai3_size", 0);
						if (kuai3_size != 0) {
							ArrayList<BallDTO> list = new ArrayList<BallDTO>();
							for (int i = 0; i < kuai3_size; i++) {
								BallDTO ball = new BallDTO();
								ball.setElv_ball(pUtil.getString("kuai3_ball" + i, ""));
								list.add(ball);
							}
							intent.putExtra("list_elvdto", list);
							intent.putExtra("AllMoney", pUtil.getInt("kuai3_allMoney", 0));
							intent.putExtra("PlayTypeID", pUtil.getInt("kuai3_play", 0));
							intent.putExtra("prizeIntellChase", pUtil.getInt("kuai3_prize", 0));
							intent.setClass(getActivity(), Kuai3ListActivity.class);
							Toast.makeText(getActivity(), R.string.this_is_the_last_time_you_save_the_number, Toast.LENGTH_SHORT).show();
						} else {
							intent.setClass(getActivity(), Kuai3Activity.class);
						}
						break;
				}
				getActivity().startActivityForResult(intent, REQ_BUYLOT_FROM_MAINHOME);
			}
		});
		tv_title.setText(dealDetailString("多多彩票"));
		lotData = new ArrayList<Map<String, Object>>();
		List<Map<String, Object>> deflotList = new ArrayList<Map<String, Object>>();
//		int numLotkinds = 5;
		int numLotkinds = 7;//with six fuck， add ssc
		if (deflotList == null || deflotList.size() < numLotkinds) {
			int lotID = 5;
			// modify by rainy 屏蔽 内蒙古快三
			for (int i = 0; i < numLotkinds; /* i < 15; */i++) {
				Map<String, Object> item = new HashMap<String, Object>();
				switch (i) {
//				case 0:
//					lotID = LOT_ID_JCZQ;
//					 item.put("LotInfo", "");
//					break;
//				case 1:
//					lotID = LOT_ID_JCLQ;
//					item.put("LotInfo", "");
//					break;
//				case 2:
//					lotID = LOT_ID_FC3D;
//					item.put("LotInfo", "");
//					break;
//				case 3:
//					lotID = LOT_ID_11X5;
//					item.put("LotInfo", "");
//					break;
//				case 4:
//					lotID = LOT_ID_11YDJ;
//					item.put("LotInfo", "");
//					break;
					case 0://with six fuck
						lotID = LOT_ID_JCZQ;
						item.put("LotInfo", "");
						break;
					case 1:
						lotID = LOT_ID_JCLQ;
						item.put("LotInfo", "");
						break;
					case 2:
						lotID = LOT_ID_FC3D;
						item.put("LotInfo", "");
						break;
					case 3:
						lotID = LOT_ID_11X5;
						item.put("LotInfo", "");
						break;
					case 4:
						lotID = LOT_ID_11YDJ;
						item.put("LotInfo", "");
						break;
					case 5:
						lotID = LOT_ID_CQSSC;
						item.put("LotInfo", "");
						break;
					case 6:
						lotID = LOT_ID_LHC;
						item.put("LotInfo", "");
						break;
				}
				item.put("LotID", lotID);
				item.put("IsSell", true);
				item.put("IsBalanceWarn", "false");
				item.put("Balance", "0");
				lotData.add(item);
			}

		} else {
			lotData.addAll(deflotList);
		}

		// 默认彩种排序
		// List<Map<String, Object>> deflotList =
		// MyPreference.getStringArrayPreflist2(getActivity(), "MH", "deflot");
		// List<Map<String, Object>> deflotList = new
		// ArrayList<Map<String,Object>>();
		// if (deflotList == null || deflotList.size() < 16) {
		// int lotID = 5;
		// //modify by rainy 屏蔽 内蒙古快三
		// for (int i = 0; i < 15; i++) {
		// switch (i) {
		// case 0:
		// lotID = LOT_ID_SSQ;
		// break;
		// case 1:
		// lotID = LOT_ID_DLT;
		// break;
		// case 2:
		// lotID = LOT_ID_JCZQ;
		// break;
		// case 3:
		// lotID = LOT_ID_KUAI3;
		// break;
		// case 4:
		// lotID = LOT_ID_11YDJ;
		// break;
		// case 5:
		// lotID = LOT_ID_11X5;
		// break;
		// case 6:
		// lotID = LOT_ID_CQSSC;
		// break;
		// case 7:
		// lotID = LOT_ID_FC3D;
		// break;
		// case 8:
		// lotID = LOT_ID_PL3;
		// break;
		// case 9:
		// lotID = LOT_ID_PL5;
		// break;
		// case 10:
		// lotID = LOT_ID_QXC;
		// break;
		// case 11:
		// lotID = LOT_ID_QLC;
		// break;
		// case 12:
		// lotID = LOT_ID_BD;
		// break;
		// case 13:
		// lotID = LOT_ID_JCLQ;
		// break;
		// case 14:
		// lotID = LOT_ID_ZQ_SFC;
		// break;
		// case 15:
		// lotID = LOT_ID_ZQ_RX9;
		// break;
		// }
		// Map<String, Object> item = new HashMap<String, Object>();
		// item.put("LotID", lotID);
		// item.put("LotInfo", "");
		// item.put("IsSell", true);
		// item.put("IsBalanceWarn", "false");
		// item.put("Balance", "0");
		// lotData.add(item);
		// }
		//
		// } else {
		// lotData.addAll(deflotList);
		// }

		adapter = new LotAdapter(getActivity(), lotData);
		lv_mListView.setAdapter(adapter);
		adapter.notifyDataSetChanged();

		// getLots();
		// getBannerViews();

	}

	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		ApplicationData.getInstance().initList();
	}

	@Override
	public void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}

	/**
	 * 获取最后一次已登录账号自动登入
	 */
	private void autoUserLogin() {
		if (false == ApplicationData.getInstance().isUserLogin()) {
			SharedPreferences sp = getActivity().getSharedPreferences("userNamesFile", Activity.MODE_PRIVATE);
			String lastUser = sp.getString("last-user", "");
			if (!"".equals(lastUser)) {
				String lastPwd = sp.getString(lastUser, "");
				if (!"".equals(lastPwd)) {
					String[] params = new String[3];
					params[0] = "101";
					params[1] = lastUser;
					params[2] = lastPwd;
					LoginLoader autoLogin = new LoginLoader();
					autoLogin.execute(params);
				}
			}
		}
	}

	public class LoginLoader extends AsyncTask<String, Integer, String> {
		String loginName;
		String loginPwd;

		protected void onPreExecute() {
		}

		@Override
		protected String doInBackground(String... params) {
			String strResult;
			try {
				String action = params[0];
				loginName = params[1];
				loginPwd = params[2];

				JSONObject param = new JSONObject();
				param.put("Name", loginName);
				param.put("Pw", loginPwd);
				strResult = GetRequestParams.getRequestResult(param.toString(), action, getActivity().getApplicationContext());
				return strResult;
			} catch (Exception e) {
				Log.e("error", e.toString());
				return null;
			}
		}

		protected void onPostExecute(String result) {
			if (result != null) {
				try {
					JSONTokener jsonParser = new JSONTokener(result);
					JSONObject js = (JSONObject) jsonParser.nextValue();
					if (Integer.parseInt(js.getString("Code")) == 0) {
						ApplicationData.getInstance().setUserName(loginName);
						ApplicationData.getInstance().setUserPassword(loginPwd);
						ApplicationData.getInstance().setUserID(js.getString("Data"));
						MyPreference.getInstance(getActivity()).setUserId(ApplicationData.getInstance().getUserID());

						((JddMainActivity) getActivity()).refreshView();
						EventManager.getInstance().onEvent(EventType.EV_BLL_UI_MAIN_LOT_REFRESH, null);
					}
				} catch (JSONException ex) {
					Log.e("error", ex.toString());
				}
			}
		}

	}

	public static String dealDetailString(String content) {
		String temp = "";
		if (content != null) {
			temp = content.replaceAll("\n", "").replaceAll("\b", "");
		}
		String str_content = (content == null ? "" : temp);
		if (str_content.length() > 12) {
			temp = str_content.substring(0, 4) + "..." + str_content.substring(str_content.length() - 4);
		} else {
			temp = str_content;
		}
		return temp;
	}

	public class LotAdapter extends BaseAdapter {

		List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
		LayoutInflater inflater = null;

		public LotAdapter(Context context, List<Map<String, Object>> data) {
			this.data = data;
			inflater = LayoutInflater.from(context);
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return data.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return data.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			convertView = inflater.inflate(R.layout.mh_lot_item, null);
			ImageView iv = (ImageView) convertView.findViewById(R.id.mh_iv_lot_icon);
			TextView tv_lot_name = (TextView) convertView.findViewById(R.id.mh_tv_lot_name);
			TextView tv_lot_info = (TextView) convertView.findViewById(R.id.mh_tv_lot_info);
			TextView tv_lot_action = (TextView) convertView.findViewById(R.id.mh_tv_lot_action);
			Map<String, Object> item = data.get(position);
			iv.setBackgroundResource(LotUtil.getIconByID(Integer.parseInt(item.get("LotID").toString())));
			tv_lot_name.setText(LotUtil.getLotNamebyID(Integer.parseInt(item.get("LotID").toString())));
			String lotinfo = item.get("LotInfo").toString();
			if (lotinfo.isEmpty()) {
				tv_lot_action.setVisibility(View.INVISIBLE);
			} else {
				tv_lot_action.setVisibility(View.VISIBLE);
				tv_lot_action.setText(lotinfo);
			}
			tv_lot_info.setText(LotUtil.getLotDesByID(Integer.parseInt(item.get("LotID").toString())));
			ImageView mh_iv_right_icon = (ImageView) convertView.findViewById(R.id.mh_iv_right_icon);
			// Logger.e(TAG, "IsBalanceWarn = " +
			// "true".equals(item.get("IsBalanceWarn").toString()) + ", [" +
			// item.get("IsBalanceWarn").toString()+"]");

			if (item.get("IsBalanceWarn") == null || item.get("Balance") == null) {
				mh_iv_right_icon.setVisibility(View.GONE);
			} else {
				if (!"true".equals(item.get("IsBalanceWarn").toString())) {
					mh_iv_right_icon.setVisibility(View.GONE);
				} else {
					int balance = (int) Float.parseFloat(item.get("Balance").toString().replace(",", ""));

					if (balance < 200000000) {
					} else if (balance < 300000000) {
						mh_iv_right_icon.setBackgroundResource(R.drawable.money_02);
					} else if (balance < 400000000) {
						mh_iv_right_icon.setBackgroundResource(R.drawable.money_03);
					} else if (balance < 500000000) {
						mh_iv_right_icon.setBackgroundResource(R.drawable.money_04);
					} else if (balance < 600000000) {
						mh_iv_right_icon.setBackgroundResource(R.drawable.money_05);
					} else if (balance < 700000000) {
						mh_iv_right_icon.setBackgroundResource(R.drawable.money_06);
					} else if (balance < 800000000) {
						mh_iv_right_icon.setBackgroundResource(R.drawable.money_07);
					} else if (balance < 900000000) {
						mh_iv_right_icon.setBackgroundResource(R.drawable.money_08);
					} else if (balance < 1000000000) {
						mh_iv_right_icon.setBackgroundResource(R.drawable.money_09);
					} else {
						mh_iv_right_icon.setBackgroundResource(R.drawable.money_10);
					}
				}
			}
			return convertView;
		}

	}

	private void getLots() {
		LotLoader loader = new LotLoader();
		loader.execute("");
	}

	private class LotLoader extends AsyncTask<String, Integer, String> {

		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
		}

		@Override
		protected String doInBackground(String... params) {
			// TODO Auto-generated method stub
			String strResult;
			String action = "123";
			String param = "{}";
			String UserID = ApplicationData.getInstance().getUserID();
			strResult = GetRequestParams.getRequestUserIDResult(param, UserID, action, getActivity().getApplicationContext());
			return strResult;
		}

		@Override
		protected void onPostExecute(String result) {
			// TODO Auto-generated method stub
			try {
				if (result != null) {
					JSONTokener jsonParser = new JSONTokener(result);
					JSONObject js = (JSONObject) jsonParser.nextValue();
					// 接下来的就是JSON对象的操作了
					if (Integer.parseInt(js.getString("Code")) == 0) {

						if (ApplicationData.getInstance().isUserLogin()) {

							lotData.clear();

							List<Map<String, Object>> lot_data = ConvertJsonToList.getList(js.getString("Data"));
							for (int i = 0; i < lot_data.size(); i++) {
								int lotid2 = Integer.parseInt(lot_data.get(i).get("ID").toString());

								Map<String, Object> item = new HashMap<String, Object>();
								item.put("LotID", lotid2);
								if (Integer.parseInt(lot_data.get(i).get("IsAddAwards").toString()) == 1) {
									item.put("LotInfo", "加奖");
								} else {
									// 获取开奖信息
									item.put("LotInfo", LotUtil.getLotInfoByID(lotid2));
								}
								// item.put("IsSell", true);
								item.put("IsSell", "0".equals(lot_data.get(i).get("IsStop").toString()));
								if (null != lot_data.get(i).get("IsBalanceWarn") && null != lot_data.get(i).get("Balance")) {
									item.put("IsBalanceWarn", lot_data.get(i).get("IsBalanceWarn").toString());
									item.put("Balance", lot_data.get(i).get("Balance").toString());
								} else {
									item.put("IsBalanceWarn", "false");
									item.put("Balance", "0");
								}
								lotData.add(item);
							}
							if (getActivity() != null)
								MyPreference.setStringArrayPreflist2(getActivity(), "MH", "deflot", lotData);
						} else {

							lotData.clear();

							List<Map<String, Object>> lot_data = ConvertJsonToList.getList(js.getString("Data"));
							for (int i = 0; i < lot_data.size(); i++) {
								int lotid2 = Integer.parseInt(lot_data.get(i).get("ID").toString());

								Map<String, Object> item = new HashMap<String, Object>();
								item.put("LotID", lotid2);
								if (Integer.parseInt(lot_data.get(i).get("IsAddAwards").toString()) == 1) {
									item.put("LotInfo", "加奖");
								} else {
									// 获取开奖信息
									item.put("LotInfo", LotUtil.getLotInfoByID(lotid2));
								}
								// item.put("IsSell", true);
								item.put("IsSell", "0".equals(lot_data.get(i).get("IsStop").toString()));
								if (null != lot_data.get(i).get("IsBalanceWarn")) {
									item.put("IsBalanceWarn", lot_data.get(i).get("IsBalanceWarn").toString());
									item.put("Balance", lot_data.get(i).get("Balance").toString());
								} else {
									item.put("IsBalanceWarn", "false");
									item.put("Balance", "0");
								}
								lotData.add(item);
							}
							if (getActivity() != null)
								MyPreference.setStringArrayPreflist2(getActivity(), "MH", "deflot", lotData);

						}
						adapter.notifyDataSetChanged();
					}

				}
			} catch (NumberFormatException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			lv_ptrl_lot.onRefreshComplete();
		}

	}
}
